<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            p{
                background-color: lightgreen;
                width:200px;
                font-size: 36px;
                line-height: 36px;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <button @click="toggle">toggle</button>
            <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter"
            @before-leave="beforeLeave"
            @leave="leave"
            @after-leave="afterLeave"
            >
                <p v-if="show">Hello</p>
            </transition>
        </div>
        <script src="vue.js"></script>
        <script>
        var vm = new Vue({
            el:'#app',
            data:{show:false},
            methods:{
                toggle(){
                    this.show = !this.show;
                },
                beforeEnter(el){
                    console.log('beforeEnter');
                    //在动画开始前先把不透明度改为0
                    el.style.opacity= 0;
                },
                enter(el,done){
                    console.log('enter');
                    let currentTime = 0;
                    const interval = 16;//1s是60帧，1帧16毫秒
                    const totalTime = 1000;//总时长是1000毫秒
                    function animate(){
                        currentTime+=interval;//16
                        const progress = currentTime/totalTime;
                        if(progress<1){
                            el.style.opacity = progress;
                            requestAnimationFrame(animate);
                        }else{
                            el.style.opacity = 1;
                            //通知Vue动画已经执行结束了
                            done();
                        }
                    }
                    requestAnimationFrame(animate);
                },
                afterEnter(el){
                    console.log('afterEnter');
                },
                beforeLeave(el){
                    console.log('beforeLeave');
                    //在动画开始前先把不透明度改为1
                    el.style.opacity= 1;
                },
                leave(el,done){
                    console.log('leave');
                    let currentTime = 0;
                    const interval = 16;//1s是60帧，1帧16毫秒
                    const totalTime = 1000;//总时长是1000毫秒
                    function animate(){
                        currentTime+=interval;//16
                        const progress = currentTime/totalTime;
                        if(progress<1){
                            el.style.opacity = 1-progress;
                            requestAnimationFrame(animate);
                        }else{
                            el.style.opacity = 0;
                            //通知Vue动画已经执行结束了
                            done();
                        }
                    }
                    requestAnimationFrame(animate);
                },
                afterLeave(el){
                    console.log('afterLeave');
                }
            }
        });
    </script>
    </body>
</html>